<!-- 商务参数配置弹窗 -->
<template>
  <el-dialog
    :title="'商务参数配置'"
    :visible="visible"
    width="67s0px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="nameValueArray" :rules="rules" label-width="220px">
      <el-form-item
        v-for="item in formData"
        :label="item.name"
        :key="item.id"
        :prop="item.key"
      >
        <!-- 这里的v-model绑定的数据是的channel_config对象的值 -->
        <!-- 然后用nameValueArray[item.key]这个确定的值，页面展示后期就不会冲突 -->
        <!-- 循环时只有的当前项key有和channel_config里对应到的时候才显示值 -->
        <el-input
          v-model="nameValueArray[item.key]"
          :placeholder="item.name"
          :disabled="item.is_null === 2"
          :type="item.type === 1 ? 'text' : 'textarea'"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "ChannelTemplate",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({}),
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      packageId: "",
      // 表单dom数据
      formData: [],
      Rules: {},
      // 表单input的值
      nameValueArray: {}
      // sdk_version_guanlian: ""
    };
  },

  watch: {
    visible() {
      if (this.data && this.visible) {
        // console.log(this.data.id, '!!DDAA')
        this.packageId = this.data.id;
        // 获取要填写的表单值
        this.$http
          .get("/Package/getPackageConfig?package_id=" + this.packageId)
          .then(res => {
            // console.log(res)
            if (res.data.code === 0) {
              res.data.data
                ? res.data.data.channel_config != null
                  ? (this.nameValueArray = res.data.data.channel_config)
                  : (this.nameValueArray = {})
                : (this.nameValueArray = {});
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch(e => {
            this.$message.error(e.message);
          });

        // 获取要渲染的弹出框和类型
        this.$http
          .get("/Channeltemplate/getAll?channel_id=" + this.data.channel_id)
          .then(res => {
            // console.log(res)
            if (res.data.code === 0) {
              if (res.data.data) {
                this.formData = res.data.data;
                this.formData.forEach(item => {
                  if (item.is_null === 1) {
                    const rule = {
                      required: true, // 将必填项设置为 true
                      message: `请输入 ${item.key}`, // 自定义错误提示信息
                      trigger: "blur" // 触发校验的事件，这里使用失去焦点时触发
                    };

                    // 将校验规则添加到数组中
                    this.Rules[item.key] = [rule];
                  }
                });
                console.log(this.Rules);
                this.rules = this.Rules;
              } else {
                this.formData = [];
                this.rules = {};
              }
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch(e => {
            this.$message.error(e.message);
          });
        this.form = Object.assign({}, this.data.id);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        // this.filteredArr
        // 遍历数组
        // this.formData.forEach(item => {
        //   // 判断对象是否有 value 字段
        //   if (Object.prototype.hasOwnProperty.call(item, "value")) {
        //     // 将满足条件的项推入新对象中
        //     this.nameValueArray[item.key] = item.value;
        //   }
        // });

        if (valid) {
          this.loading = true;
          this.form.package_id = this.form.id;
          this.$http
            .post(
              `/package/setPackageSdkConfig?package_id=${this.packageId}`,
              { channel_config: this.nameValueArray }
            )
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.formData = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
      this.rules = {};
      console.log(this.rules);
      // console.log(this.form, '!!')
    }
  }
};
</script>

<style scoped></style>
